package presentation;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.WindowConstants;
import com.cloudgarden.layout.AnchorLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;

/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class UpdateUserForm extends javax.swing.JDialog {

	private static final long serialVersionUID = 3487576749815702846L;

	private String user;
	private JLabel nameLabel;
	private JLabel pwdLabel;
	private JPasswordField oldPassField;
	private JPasswordField newPassField1;
	private JPasswordField newPassField2;
	private JLabel jLabel2;
	private JLabel emailLabel;
	private JTextField newEmailField;
	private JLabel jLabel1;
	private JButton cancelButton;
	private JButton saveButon;

	public UpdateUserForm(JFrame frame,String username) {
		super(frame);
		this.getParent().setEnabled(false);
		this.user = username;
		initGUI();
	}

	private void initGUI() {
		try {
			{
				GridBagLayout thisLayout = new GridBagLayout();
				thisLayout.rowWeights = new double[] {0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.1, 0.1, 0.1};
				thisLayout.rowHeights = new int[] {6, 31, 25, 7, 25, 7, 25, 7, 25, 20, 7, 20};
				thisLayout.columnWeights = new double[] {0.1, 0.1, 0.1, 0.1, 0.0, 0.0};
				thisLayout.columnWidths = new int[] {7, 7, 7, 7, 49, 19};
				this.setTitle("Change your Details");
				getContentPane().setLayout(thisLayout);
				this.setAlwaysOnTop(true);
				this.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
				this.addWindowListener(closeHandler(this));
				this.setResizable(false);
				{
					cancelButton = new JButton();
					getContentPane().add(cancelButton, new GridBagConstraints(3, 10, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.VERTICAL, new Insets(0, 0, 0, 0), 0, 0));
					cancelButton.setText("Cancel");
					cancelButton.setPreferredSize(new java.awt.Dimension(103, 32));
					cancelButton.setFont(new java.awt.Font("Aharoni",0,16));
					cancelButton.addActionListener(cancelHandler(this));
				}
				{
					saveButon = new JButton();
					GridBagLayout saveButonLayout = new GridBagLayout();
					getContentPane().add(saveButon, new GridBagConstraints(1, 10, 1, 1, 0.0, 0.0, GridBagConstraints.EAST, GridBagConstraints.VERTICAL, new Insets(0, 0, 0, 0), 0, 0));
					saveButon.setText("Save");
					saveButon.setPreferredSize(new java.awt.Dimension(103, 33));
					saveButon.setFont(new java.awt.Font("Aharoni",0,16));
					saveButonLayout.rowWeights = new double[] {0.1, 0.1, 0.1, 0.1};
					saveButonLayout.rowHeights = new int[] {7, 7, 7, 7};
					saveButonLayout.columnWeights = new double[] {0.1, 0.1, 0.1, 0.1};
					saveButonLayout.columnWidths = new int[] {7, 7, 7, 7};
					saveButon.setLayout(saveButonLayout);
					this.saveButon.addActionListener(saveHandler(this));
				}
				{
					oldPassField = new JPasswordField();
					getContentPane().add(oldPassField, new GridBagConstraints(2, 2, 3, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0));
					oldPassField.setPreferredSize(new java.awt.Dimension(195, 21));
				}
				{
					pwdLabel = new JLabel();
					@SuppressWarnings("unused")
					AnchorLayout pwdLabelLayout = new AnchorLayout();
					getContentPane().add(pwdLabel, new GridBagConstraints(1, 2, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
					pwdLabel.setText("Old Password:");
					pwdLabel.setPreferredSize(new java.awt.Dimension(122, 21));
					pwdLabel.setLayout(null);
					pwdLabel.setFont(new java.awt.Font("Tahoma",0,16));
				}
				{
					nameLabel = new JLabel();
					@SuppressWarnings("unused")
					AnchorLayout nameLabelLayout = new AnchorLayout();
					getContentPane().add(nameLabel, new GridBagConstraints(1, 1, 3, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
					nameLabel.setText("Username: "+this.user);
					nameLabel.setPreferredSize(new java.awt.Dimension(191, 41));
					nameLabel.setLayout(null);
					nameLabel.setFont(new java.awt.Font("Tahoma",1,16));
				}
				{
					newPassField1 = new JPasswordField();
					getContentPane().add(newPassField1, new GridBagConstraints(2, 6, 3, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0));
					newPassField1.setPreferredSize(new java.awt.Dimension(195, 21));
				}
				{
					jLabel1 = new JLabel();
					getContentPane().add(jLabel1, new GridBagConstraints(1, 6, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
					jLabel1.setText("New Password:");
					jLabel1.setFont(new java.awt.Font("Tahoma",0,16));
					jLabel1.setPreferredSize(new java.awt.Dimension(118, 24));
					jLabel1.setLayout(null);
				}
				{
					newPassField2 = new JPasswordField();
					getContentPane().add(newPassField2, new GridBagConstraints(2, 8, 3, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0));
					newPassField2.setPreferredSize(new java.awt.Dimension(195, 21));
				}
				{
					jLabel2 = new JLabel();
					getContentPane().add(jLabel2, new GridBagConstraints(1, 8, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
					jLabel2.setText("Confirm Pass:");
					jLabel2.setFont(new java.awt.Font("Tahoma",0,16));
					jLabel2.setPreferredSize(new java.awt.Dimension(106, 24));
					jLabel2.setLayout(null);
				}
				{
					emailLabel = new JLabel();
					getContentPane().add(emailLabel, new GridBagConstraints(1, 4, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
					emailLabel.setText("Email(optional):");
					emailLabel.setFont(new java.awt.Font("Tahoma",0,16));
				}
				{
					newEmailField = new JTextField();
					getContentPane().add(newEmailField, new GridBagConstraints(2, 4, 3, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0));
				}
			}
			pack();
			this.setVisible(true);
		} catch (Exception e) {
			System.out.println(e.getMessage());
		}
	}


	/**
	 * enable my owner (meening - the JFrame that is passed to the Constructor)
	 * and close myself
	 */
	private void close() {
		this.getParent().setEnabled(true);
		this.dispose();
	}


	/**
	 * return an Action Listener. this Listener gets this LoginForm
	 * and invoke its close() method
	 * @param updatePassForm - the LoginForm where the button is in
	 * @return an action listener
	 */
	private ActionListener cancelHandler(final UpdateUserForm updatePassForm) {
		return new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				updatePassForm.close();
			}
		};
	}

	/**
	 * return an Action Listener. this Listener gets this LoginForm
	 * and invoke its close() method
	 * @param updatePassForm - the LoginForm where the button is in
	 * @return an action listener
	 */
	private ActionListener saveHandler(final UpdateUserForm me) {
		return new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				WsepGUI tGUI = (WsepGUI)me.getParent();
				String oldPass = new String(me.oldPassField.getPassword());
				String newPass1 = new String(me.newPassField1.getPassword());
				String newPass2 = new String(me.newPassField2.getPassword());
				String email = newEmailField.getText();
				if(newPass1.equals(newPass2)){
					tGUI.updateUser(me, email, oldPass, newPass1);
					me.close();
				}
				else{
					me.oldPassField.setText("");
					me.newPassField1.setText("");
					me.newPassField2.setText("");
					me.newEmailField.setText("");
				}
				
			}
		};
	}

	/**
	 * return a Window Listener. this Listener gets this LoginForm
	 * only the WindowClosed() method is implemented and it invoke me.close()
	 * @param updatePassForm - the LoginForm where the button is in
	 * @return a Window listener
	 */
	private WindowListener closeHandler(final UpdateUserForm updatePassForm) {
		return new WindowListener() {
			public void windowActivated(WindowEvent e) {
				// TODO Auto-generated method stub

			}
			public void windowClosed(WindowEvent e) {
				updatePassForm.close();
			}
			public void windowClosing(WindowEvent e) {
				// TODO Auto-generated method stub

			}
			public void windowDeactivated(WindowEvent e) {
				// TODO Auto-generated method stub

			}
			public void windowDeiconified(WindowEvent e) {
				// TODO Auto-generated method stub

			}
			public void windowIconified(WindowEvent e) {
				// TODO Auto-generated method stub

			}
			public void windowOpened(WindowEvent e) {
				// TODO Auto-generated method stub

			}
		};
	}
	
	public JTextField getNewEmailFeild() {
		return newEmailField;
	}

}